<template>
  <div class="container">
    <div class="nav">
      <van-nav-bar title="地址列表" @click-left="goBack">
        <template #left>
          <van-icon name="arrow-left" size="1rem" color="black" />
        </template>
      </van-nav-bar>
    </div>
    <div class="content">
      <van-address-list
        v-model="chosenAddressId"
        :list="list"
        :disabled-list="disabledList"
        :switchable="false"
        disabled-text="以下地址不可用"
        default-tag-text="默认"
        @add="onEdit"
        @edit="onEdit"
        @click-item="itemClick"
      />
    </div>
  </div>
</template>
    
<script>
import { AddressList, NavBar, Icon } from "vant";
export default {
  components: {
    "van-address-list": AddressList,
    "van-nav-bar": NavBar,
    "van-icon": Icon
  },
  data() {
    return {
      chosenAddressId: "1",
      list: [
        {
          id: "1",
          name: "张三",
          tel: "13000000000",
          address: "浙江省杭州市西湖区文三路 138 号东方通信大厦 7 楼 501 室",
          isDefault: true
        },
        {
          id: "2",
          name: "李四",
          tel: "1310000000",
          address: "浙江省杭州市拱墅区莫干山路 50 号"
        }
      ],
      disabledList: [
        {
          id: "3",
          name: "王五",
          tel: "1320000000",
          address: "浙江省杭州市滨江区江南大道 15 号"
        }
      ]
    };
  },
  methods: {
    onEdit(item) {
      let url = !item ? `/address/edit/0` : `/address/edit/${item.id}`;
      this.$router.push(url);
    },
    goBack() {
      console.log(this.$route.query.back);
      this.$router.push(this.$route.query.back);
    },
    itemClick(item) {
      console.log(item);
      sessionStorage.setItem("selectedAddress", JSON.stringify(item));
      this.$router.push(this.$route.query.back);
    }
  }
};
</script>

<style lang="less">
.container {
  height: 100%;
  background-color: #f7f7f7;
  .nav {
    height: 2.3rem;
  }
  .content {
    height: calc(100% - 2.3rem);
  }
}
</style>